package com.leave.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leave.entity.Menu;
import com.leave.entity.Role;
import com.leave.service.IMenuService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class MenuController {
	@Autowired
	private IMenuService menuService;
	
	
	@RequestMapping("/getAllParentMenuByPage")
	@ResponseBody
	public String getAllParentMenuByPage(Integer page,Integer rows) {
		HashMap<String,Object> map = menuService.getAllParentMenuByPage(page, rows);
		List<Menu> menuList = (List<Menu>) map.get("menuList");
		JSONArray jsArr = JSONArray.fromObject(menuList);
		JSONObject js = new JSONObject();
		js.put("total", map.get("total"));
		js.put("rows", jsArr);
		return js.toString();
	}
	
	@RequestMapping(value="/getMenuByMenuId",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getMenuByMenuId(int menuId) {
		Menu menu = menuService.getMenuByMenuId(menuId);
		JSONObject js = new JSONObject();
		js.put("menu", menu);
		return js.toString();
	}
	
	@RequestMapping("/removeMenuByArrMenuId")
	@ResponseBody
	public String removeMenuByArrMenuId(int[] arrId) {
		int i = menuService.removeMenuByArrId(arrId);
		if(i == arrId.length) {
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("/addMenu")
	@ResponseBody
	public String addMenu(Menu m ) {
		Menu menu = menuService.getMenuByMenuName(m.getMenuName());
		if(menu !=null) {
			return "exist";
		}
		m.setMenuIsparent(true);
		int i = menuService.addMenu(m);
		if(i > 0) {
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("/updateMenuByMenu")
	@ResponseBody
	public String updateMenuByMenu(Menu m ) {
		Menu menu = menuService.getMenuByMenuName(m.getMenuName());
		if(menu !=null&&!menu.getMenuName().equals(m.getMenuName())) {
			return "exist";
		}
		int i = menuService.updateMenuByMenu(m);
		if(i > 0) {
			return "success";
		}
		return "false";
	}
	
	
	
	
	
	
	
}
